package net.kindroid.fm;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class FMProvider extends ContentProvider
{
	/**
	 * tag
	 */
	private static final String tag = "FMProvider";

	/**
	 * for table
	 */
	private static final int FM_LIST = 1;

	private static final UriMatcher sUriMatcher;

	static
	{
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(FMInfo.AUTHORITY, "FMTable", FM_LIST);
	}

	private DatabaseHelper mOpenHelper;
	private SQLiteDatabase mDB;

	@Override
	public boolean onCreate()
	{
		mOpenHelper = new DatabaseHelper(getContext());
		return true;
	}

	@Override
	public String getType(Uri uri)
	{
		switch (sUriMatcher.match(uri))
		{
			case FM_LIST:
			{
				return FMInfo.FMTable.CONTENT_TYPE;
			}
			default:
			{
				throw new IllegalArgumentException("Unknown URI " + uri);
			}
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values)
	{
		mDB = mOpenHelper.getWritableDatabase();
		switch (sUriMatcher.match(uri))
		{
			case FM_LIST:
			{
				long rowId = mDB.insert(FMInfo.TABLE_FM, null, values);
				if (rowId > 0)
				{
					Uri noteUri = ContentUris.withAppendedId(FMInfo.FMTable.CONTENT_URI, rowId);
					getContext().getContentResolver().notifyChange(noteUri, null);
					return noteUri;
				}
				throw new SQLException("Failed to insert row into " + uri);
			}
			default:
			{
				Log.e(tag, "Unknown URI " + uri);
				throw new IllegalArgumentException("Unknown URI " + uri);
			}
		}
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
	{
		mDB = mOpenHelper.getReadableDatabase();
		Cursor cursor = null;
		switch (sUriMatcher.match(uri))
		{
			case FM_LIST:
			{
				try
				{
					cursor = mDB.query(FMInfo.TABLE_FM, projection, selection, selectionArgs, null, null, sortOrder);
					cursor.setNotificationUri(getContext().getContentResolver(), uri);
				} catch (Exception e)
				{
					if (cursor != null)
					{
						cursor.close();
						cursor = null;
					}
				}
				return cursor;
			}
			default:
			{
				throw new IllegalArgumentException("Unknown URI " + uri);
			}
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
	{
		mDB = mOpenHelper.getWritableDatabase();
		int count = -1;
		switch (sUriMatcher.match(uri))
		{
			case FM_LIST:
			{
				count = mDB.update(FMInfo.TABLE_FM, values, selection, selectionArgs);
				break;
			}
			default:
			{
				throw new IllegalArgumentException("Unknown URI " + uri);
			}
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs)
	{
		mDB = mOpenHelper.getWritableDatabase();
		int count;
		switch (sUriMatcher.match(uri))
		{
			case FM_LIST:
			{
				count = mDB.delete(FMInfo.TABLE_FM, selection, selectionArgs);
				break;
			}
			default:
			{
				throw new IllegalArgumentException("Unknown URI " + uri);
			}
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	/**
	 * This class helps open, create, and upgrade the database file
	 * 
	 * @author tanlp
	 */
	private class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context)
		{
			super(context, FMInfo.DATABASE_NAME, null, FMInfo.DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db)
		{
			db.execSQL("CREATE TABLE " + FMInfo.TABLE_FM + " (" + FMInfo.FMTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + FMInfo.FMTable.TUNE + "  INTEGER, " + FMInfo.FMTable.TITLE + " TEXT, "
					+ FMInfo.FMTable.ALARM + " TEXT, " + FMInfo.FMTable.COMMENT + " TEXT " + ");");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
		{
			Log.w(tag, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS " + FMInfo.TABLE_FM);
			onCreate(db);
		}

	}
}
